﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using System.ComponentModel;
using System.Web.Services.Protocols;
using System.Data.Linq;
using Billing.DataObjects;
//using Billing.PaymentServices.ReportingService;
using Microsoft.SqlServer.ReportingServices2005.Execution;
using System.Configuration;
using System.Web;

namespace Billing.PaymentServices
{
    [WebService(Namespace = "http://Billing/Services/2008/08", Name = "CashDeskService")]
    [ToolboxItem(false)]
    public class CashDeskService : ICashDeskService
    {
        Billing.Services.BillingSoapHeader billingSoapHeader;
        Billing.DataObjects.BillingDataContext context = new Billing.DataObjects.BillingDataContext();

        public Billing.Services.BillingSoapHeader BillingSoapHeader
        {
            get
            {
                return this.billingSoapHeader;
            }
            set
            {
                this.billingSoapHeader = value;
            }
        }

        public ClientsInfo GetClientInfo(ref PaymentObject paymentObject)
        {
            try
            {
                ClientsInfo result = new ClientsInfo();
                ISingleResult<Billing.DataObjects.ClientsView> clients = context.CashDeskSelectClients(paymentObject.SearchKey, (short)paymentObject.SearchType, BillingSoapHeader.UserName);

                int i = 0;
                foreach (Billing.DataObjects.ClientsView cv in clients)
                {
                    ClientInfo ci = new ClientInfo();
                    CopyProps(cv, ci);
                    paymentObject.ClientInfo = ci;
                    result.Add(ci);
                    i++;
                }
                if (i != 1) paymentObject.ClientInfo = null;
                return result;
            }
            catch (Exception x)
            {
                throw BillingException.ConvertToSoapException(
                    x,
                    HttpContext.Current);
            }
        }

        public void GetBillInfo(ref PaymentObject paymentObject)
        {
            try
            {
                paymentObject.ClientInfo.PaymentItems = new PaymentItems();

                ISingleResult<AccountsView> accSelectResult = context.CashDeskSelectAccounts(paymentObject.ClientInfo.ID, paymentObject.SearchKey, BillingSoapHeader.UserName);

                foreach (AccountsView acc in accSelectResult)
                {
                    paymentObject.ClientInfo.PaymentItems.Add(new PaymentItem()
                    {
                        BillMoney = acc.Balance.Value,
                        Description = acc.AccountType,
                        Key = acc.ID.ToString(),
                        Number = acc.AccountNumber,
                        OwnerID = acc.ClientID.ToString()
                    });
                }
            }
            catch (Exception x)
            {
                throw BillingException.ConvertToSoapException(
                    x,
                    HttpContext.Current);
            }
        }

        public void Pay(PaymentObject paymentObject)
        {
            try
            {
                foreach (PaymentItem bi in paymentObject.ClientInfo.PaymentItems)
                {
                    context.CashDeskPay(Convert.ToInt64(bi.Key), paymentObject.PaymentOrderID, bi.PaidMoney, BillingSoapHeader.UserName);
                }
            }
            catch (Exception x)
            {
                throw BillingException.ConvertToSoapException(
                    x,
                    HttpContext.Current);
            }
        }

        private void CopyProps(object src, object dest)
        {
            if (src == null)
            {
                dest = null;
                return;
            }
            Type srcType = src.GetType();
            Type destType = dest.GetType();

            foreach (System.Reflection.PropertyInfo pInf in srcType.GetProperties())
            {
                System.Reflection.PropertyInfo pdi = destType.GetProperty(pInf.Name);
                if (pdi != null)
                {
                    if (pdi.PropertyType.IsAssignableFrom(pInf.PropertyType))
                    {
                        pdi.SetValue(dest, pInf.GetValue(src, null), null);
                    }
                }
            }

        }

        public byte[] GetBillDetails(PaymentObject paymentObject)
        {
            throw BillingException.CreateSoapException("", "Not implemented", -1, HttpContext.Current);

            //ReportExecutionService rs = new ReportExecutionService();

            //rs.Url = ConfigurationManager.AppSettings["ReportService"];
            //rs.Credentials = new System.Net.NetworkCredential(
            //        ConfigurationManager.AppSettings["ReportServiceUser"],
            //        ConfigurationManager.AppSettings["ReportServicePassword"]);

            //ExecutionInfo eInf = rs.LoadReport("/BillingReports/CashDeskLastDetails", null);

            //string deviceInfo = "";

            //Warning[] warnings;
            //string mimeType;
            //string encoding;
            //string[] streams;
            //string extension;

            //ParameterValue[] parameterevalues = new ParameterValue[2];
            //parameterevalues[0] = new ParameterValue();
            //parameterevalues[0].Name = "clientID";
            //parameterevalues[0].Value = paymentObject.ClientInfo.ID.ToString();

            //parameterevalues[1] = new ParameterValue();
            //parameterevalues[1].Name = "accountNumber";
            //parameterevalues[1].Value = paymentObject.ClientInfo.PaymentItems[0].Number;

            //rs.SetExecutionParameters(parameterevalues, "ka-GE");

            //byte[] buffer = rs.Render("HTML4.0", deviceInfo, out extension, out mimeType, out encoding, out warnings, out streams);

            //return buffer;
        }

        public byte[] GetPaymentOrder(ref PaymentObject paymentObject)
        {
            throw BillingException.CreateSoapException("", "Not implemented", -1, HttpContext.Current);
            //try
            //{
            //    long? paymentOrderID = null;
            //    context.CashDeskPaymentOrder(paymentObject.ClientInfo.ID, (paymentObject.ClientInfo.PaymentItems.Count == 1) ? paymentObject.ClientInfo.PaymentItems[0].Number : "",
            //        BillingSoapHeader.PaymentPointID, paymentObject.ClientInfo.PaymentItems[0].PaidMoney, ref paymentOrderID);

            //    paymentObject.PaymentOrderID = paymentOrderID.Value;

            //    ReportExecutionService rs = new ReportExecutionService();

            //    rs.Url = ConfigurationManager.AppSettings["ReportService"];
            //    rs.Credentials = new System.Net.NetworkCredential(
            //            ConfigurationManager.AppSettings["ReportServiceUser"],
            //            ConfigurationManager.AppSettings["ReportServicePassword"]);

            //    ExecutionInfo eInf = rs.LoadReport("/BillingReports/CashDeskPaymentOrder", null);

            //    ParameterValue[] parameterevalues = new ParameterValue[1];
            //    parameterevalues[0] = new ParameterValue();
            //    parameterevalues[0].Name = eInf.Parameters[0].Name;
            //    parameterevalues[0].Value = paymentObject.PaymentOrderID.ToString();

            //    rs.SetExecutionParameters(parameterevalues, "ka-GE");

            //    string deviceInfo = "";

            //    Warning[] warnings;
            //    string mimeType;
            //    string encoding;
            //    string[] streams;
            //    string extension;

            //    byte[] buffer = rs.Render("HTML4.0", deviceInfo, out extension, out mimeType, out encoding, out warnings, out streams);

            //    return buffer;
            //}
            //catch (Exception X)
            //{
            //    throw X;
            //}
        }

        public long? CreatePaymentOrder(PaymentObject paymentObject)
        {
            try
            {
                long? paymentOrderID = null;
                string description = null;
                if (paymentObject.ClientInfo.ClientTypeID == 1)
                    description = string.Format("გადახდა ტელეფონზე {0}", paymentObject.SearchKey);
                else
                    if (paymentObject.ClientInfo.ClientTypeID == 2 && paymentObject.SearchType == SearchType.ByPhoneNumber)
                        description = string.Format("გადახდა ტელეფონზე {0}", paymentObject.SearchKey);

                context.CashDeskPaymentOrder(paymentObject.ClientInfo.ID, (paymentObject.ClientInfo.PaymentItems.Count == 1) ? paymentObject.ClientInfo.PaymentItems[0].Number : "",
                    BillingSoapHeader.PaymentPointID, paymentObject.ClientInfo.PaymentItems[0].PaidMoney, ref paymentOrderID, description, BillingSoapHeader.UserName);

                paymentObject.PaymentOrderID = paymentOrderID.Value;
                return paymentOrderID;
            }
            catch (Exception x)
            {
                throw BillingException.ConvertToSoapException(
                    x,
                    HttpContext.Current);
            }

        }
    }
}
